#include "gamecoin.h"
#include <QDebug>
GameCoin::GameCoin(QString img, QWidget *parent)
    :QPushButton(parent)
    ,m_minIndex(1)
    ,m_maxIndex(8)
{
    if(img != ""){
        QPixmap normalPix;
        bool ret = normalPix.load(img);
        if(ret){
            QSize pSize(normalPix.width(),normalPix.height());
            this->setFixedSize(pSize);
            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(normalPix);
            this->setIconSize(pSize);
        }
    }
    //金币翻转
    m_timerToFront = new QTimer(this);
    m_timerToBack = new QTimer(this);
    connect(m_timerToFront, &QTimer::timeout, this, [=](){
        QString imgCoinStr = QString(":/res/Coin000%1.png").arg(--m_maxIndex);
        QPixmap imgPix;
        bool ret = imgPix.load(imgCoinStr);
        if (ret){
            QSize pSize(imgPix.width(),imgPix.height());
            this->setFixedSize(pSize);
            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(imgPix);
            this->setIconSize(pSize);
        }
        if (m_minIndex > m_maxIndex){
            m_maxIndex = 8;
            m_timerToFront->stop();
        }
    });

    connect(m_timerToBack, &QTimer::timeout, this, [=](){
        QString imgCoinStr = QString(":/res/Coin000%1.png").arg(++m_minIndex);
        QPixmap imgPix;
        bool ret = imgPix.load(imgCoinStr);
        if (ret){
            QSize pSize(imgPix.width(),imgPix.height());
            this->setFixedSize(pSize);
            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(imgPix);
            this->setIconSize(pSize);
        }
        if (m_minIndex > m_maxIndex){
            m_minIndex = 1;
            m_timerToBack->stop();
        }
    });


}

int GameCoin::posX() const
{
    return m_posX;
}

void GameCoin::setPosX(int newPosx)
{
    m_posX = newPosx;
}

int GameCoin::posY() const
{
    return m_posY;
}

void GameCoin::setPosY(int newPosY)
{
    m_posY = newPosY;
}

bool GameCoin::flag() const
{
    return m_flag;
}

void GameCoin::setFlag(bool newFlag)
{
    m_flag = newFlag;
}

void GameCoin::changeFlag()
{
    if (m_flag)
    {
        m_timerToBack->start(30);
        m_flag = false;
    }
    else{
        m_timerToFront->start(30);
        m_flag = true;
    }
}

void GameCoin::setIsWin(bool newIsWin)
{
    m_isWin = newIsWin;
}



